package com.yupi.springbootinit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.common.Result;
import com.yupi.springbootinit.emun.RedisEmnu;
import com.yupi.springbootinit.mapper.ConurseClassifyMapper;
import com.yupi.springbootinit.model.vo.ConurseClassifyReponseBody;
import com.yupi.springbootinit.service.ConurseClassifyService;
import com.yupi.springbootinit.model.entity.ConurseClassify;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author 15312
* @description 针对表【conurse_classify(表分类)】的数据库操作Service实现
* @createDate 2024-05-15 16:10:15
*/
@Service
public class ConurseClassifyServiceImpl extends ServiceImpl<ConurseClassifyMapper, ConurseClassify>
    implements ConurseClassifyService {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public List<ConurseClassifyReponseBody> getConurseClassifyList() {
        LambdaQueryWrapper<ConurseClassify> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ConurseClassify::getParentId, 0);
        return dataConversion(baseMapper.selectList(wrapper));


    }

    /*
    * 获取指定一级分类下的二级分类
    *
    * */
    @Override
    public List<ConurseClassifyReponseBody> getConurseClassifySecondList(Long id) {
        LambdaQueryWrapper<ConurseClassify> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ConurseClassify::getParentId, id);
        return dataConversion(baseMapper.selectList(wrapper));
    }

    /**
     *
     * @return 返回分类的一整个树形结构
     */
    @Override
    public Result<List<Tree<String>>> getClassifyTreeList() {
        // 查询Redis里面有没有,如果没有就存进去
        if (Boolean.FALSE.equals(redisTemplate.hasKey(RedisEmnu.CLASSTREE_REDIS_KEY.getRedisKey()))) {
            // 数据
            List<TreeNode<String>> nodeList = CollUtil.newArrayList();
            List<ConurseClassify> list = baseMapper.selectList(null);
            if (CollUtil.isNotEmpty(list)) {
                list.forEach(item -> {
                    nodeList.add(new TreeNode<>(item.getId().toString(), item.getParentId().toString(), item.getName(), item.getCascadeOrder()));
                });
                redisTemplate.opsForValue().set(RedisEmnu.CLASSTREE_REDIS_KEY.getRedisKey(), TreeUtil.build(list, "0", (treeNode, tree) -> {
                tree.setId(treeNode.getId().toString());
                tree.setParentId(treeNode.getParentId().toString());
                tree.setName(treeNode.getName());
                tree.setWeight(treeNode.getCascadeOrder());
                tree.putExtra("cascadeImg", treeNode.getCascadeImg());
                tree.putExtra("icon", treeNode.getIcon());
                tree.putExtra("createTime", treeNode.getCreateTime());
                tree.putExtra("cascadeDescribe", treeNode.getCascadeDescribe());

            }));
            }else {
                return Result.success(null);
            }
        }
        return Result.success((List<Tree<String>>) redisTemplate.opsForValue().get(RedisEmnu.CLASSTREE_REDIS_KEY.getRedisKey()));
    }

    public List<ConurseClassifyReponseBody> dataConversion(List<ConurseClassify> list) {
        List<ConurseClassifyReponseBody> conurseClassifyReponseBodies = new ArrayList<>();
        list.forEach(item -> conurseClassifyReponseBodies.add(BeanUtil.toBean(item, ConurseClassifyReponseBody.class)));
        return conurseClassifyReponseBodies;
    }
}




